import java.sql.*;

public class JdbcDemo01 {
    public static void main(String[] args) throws SQLException {
        // 创建驱动对象
        Driver driver = new com.mysql.jdbc.Driver();
        // 1、注册驱动
        DriverManager.registerDriver(driver);
        // 2、获取连接
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_16_01?characterEncoding=UTF-8", "root", "123456");

        //3、获取执行SQL对象
        Statement statement = connection.createStatement();

        // 4、执行sql
        String sql = "select * from student";
        ResultSet resultSet = statement.executeQuery(sql);

        // 5、处理结果
        while (resultSet.next()) {
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            String sex = resultSet.getString("sex");
            String birth = resultSet.getString("birth");
            String department = resultSet.getString("department");
            String address = resultSet.getString("address");
            System.out.println(id + "-" + name + "-" + sex + "-" + birth + "-" + department + "-" + address);
        }
        // 6、释放资源
        if (resultSet != null) {
            resultSet.close();
        }
        if (statement != null) {
            statement.close();
        }
        if (connection != null) {
            connection.close();
        }

    }
}
